<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>扣子测试</title>
</head>
<body>
<div id="msg">输出：</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- <script src="https://lf-cdn.coze.cn/obj/unpkg/flow-platform/chat-app-sdk/1.1.0-beta.0/libs/cn/index.js"></script> -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
<script>
    // new CozeWebSDK.WebChatClient({
    //   config: {
    //     bot_id: '7452915373378781219',
    //   },
    //   componentProps: {
    //     title: 'Coze',
    //   },
    // });
    // setTimeout(function(){
    //   $('img').css('width', '200px').css('height', '200px')
    //             .css('position', 'fixed').css('right', '100px').css('bottom', '50px');
    // }, 1000);

    (function(){
      signa();
    })();

    // 获取密钥
    function signa(){
      $.ajax({
        type: 'get',
        url: 'https://shop.micropig.cn/index.php?s=/api/robot/xunfei&wxapp_id=10002',
        data: {},
        success: function(res){
          console.log(res);
          var ts = res.data.ts;
          var signa = res.data.signa;
          ws({ts: ts, signa: signa});
        }
      });
    }

    // 长连接
    function ws(e){
      // 创建一个Socket实例
      var appid = '60328d1e';
//      var apikey = '004e6f0d3025348661f93e680dbf0241';
//      var timestamp = Date.now();
      var timestamp = e.ts;
      var wsUrl = 'wss://rtasr.xfyun.cn/v1/ws';
      wsUrl += '?appid=' + appid;
      wsUrl += '&ts=' + timestamp;
      wsUrl += '&signa=' + e.signa;
      wsUrl += '&pd=edu';
      var socket = new WebSocket(wsUrl);
      // 当WebSocket创建成功时触发
      socket.onopen = function(event) {
        // 发送一个初始化消息或者其他消息
        socket.send('Hello, Server!');
      };
      // 当服务器发送信息时触发
      socket.onmessage = function(event) {
        console.log(event);
        // 输出服务器发送的消息
        console.log('Message from server:', event.data);
        var msgDiv = $('#msg');
        msgDiv.html(msgDiv.html() + event.data);
      };
      // 当WebSocket通信发生错误时触发
      socket.onerror = function(error) {
        console.log('WebSocket error:', error);
      };
      // 当WebSocket连接关闭时触发
      socket.onclose = function() {
        console.log('WebSocket connection closed');
      };
    }
</script>
</body>
</html>